package com.zuiacsn.java_gobang.game;

import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;

import java.util.concurrent.ConcurrentHashMap;

@Component
public class OnlineUserManager {
    // 使用这个哈希表来表示当前用户在游戏大厅的在线状态 key表示 用户id value表示 webSocket会话
    private ConcurrentHashMap<Integer, WebSocketSession> gameHall = new ConcurrentHashMap<Integer,WebSocketSession>(); // 保证线程安全
    // 当前这个哈希表表示用户在游戏房间中的在线状态
    private ConcurrentHashMap<Integer, WebSocketSession> gameRoom = new ConcurrentHashMap<Integer, WebSocketSession>();


    public void enterGameHall(int userId,WebSocketSession session){
        gameHall.put(userId, session);
    }

    public void exitGameHall(int userId){
        gameHall.remove(userId);
    }

    public WebSocketSession getFromGall(int userId){
        return gameHall.get(userId);
    }


    public void enterGameRoom(int userId,WebSocketSession session){
        gameRoom.put(userId, session);
    }

    public void exitGameRoom(int userId){
        gameRoom.remove(userId);
    }

    public WebSocketSession getFromRoom(int userId){
        return gameRoom.get(userId);
    }

}
